// pages/xinZengDan/order3/order.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderCheck: null,
    username: null,
    stepsIndex: 2,
    stepItemCount: 4,
    stepItemContent: [],
    stepItemContent2: [],
    peitaoList: [
      { label: '常规五金配套', value: '1' },
      { label: '单要防火锁', value: '2' },
      { label: '单要闭门器', value: '3' },
      { label: '单要拉爆盖', value: '4' },
      { label: '不含五金', value: '5' },
    ],
    peitaoSelect: '常规五金配套',
    peitaoSelectValue: 1,
    peitao2Visible: false,
    modalHidden: true,
    shangpinList: [],
    shangpinSelect: [],
    zongjine: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      orderCheck: options.key,
      username: options.username,
      stepItemCount: app.globalData.stepItemCount,
      stepItemContent: app.globalData.stepItemContent,
      stepItemContent2: app.globalData.stepItemContent2,
      shouhuoDizhi: null
    })
    wx.request({
      url: 'https://127.0.0.1:10270/api/shangpinListGet',
      method: 'POST',
      timeout: 4000,
      success: (result) => {
        // console.log(result);
        this.setData({
          shangpinList: result.data
        })
      },
      fail: (err) => { },
      complete: (res) => { },
    })
  },
  onIconTap: function () {
    this.setData({ modalHidden: false })
  },
  unOnIconTap: function () {
    this.setData({
      shangpinSelect: []
    })
  },
  GoodClick: function (params) {
    var that = this
    var id = params.currentTarget.dataset.id
    // console.log(params.currentTarget.dataset.id);
    // console.log(this.data.shangpinList[id]);
    if (this.data.orderCheck == 2) {
      var list = this.data.shangpinSelect
      var item = this.data.shangpinList[id]
      wx.request({
        url: 'https://127.0.0.1:10270/api/shangPinTianJia?danhao='
          + item.shangpinbianhao + "&wujinSet=" + this.data.peitaoSelectValue,
        method: "post",
        success: (result) => {
          // console.log(result.data);
          var listReq = result.data
          var checkKuan = 0
          listReq.forEach((element, index) => {
            element.shangpinshuliang = 1
            if (checkKuan != 0 && checkKuan != element.shangpinguigekuan) {
              switch (element.shangpinleibie) {
                case "防火锁":
                  element.shangpinshuliang = 1
                  break;
                case "闭门器":
                  if (element.shangpinguigekuan > 1000) {
                    element.shangpinshuliang = 2
                  } else {
                    element.shangpinshuliang = 1
                  }
                  break;
                case "拉爆盖":
                  element.shangpinshuliang = 6
                  break;
                default:
                  break;
              }
            }
            if (index == 0) {
              checkKuan = element.shangpinguigekuan
            }
          });
          list.forEach(element1 => {
            var indexOfReq = -1
            listReq.forEach((element2, index) => {
              if (element1.shangpinbianhao == element2.shangpinbianhao) {
                element1.shangpinshuliang += Number(element2.shangpinshuliang)
                indexOfReq = index
              }
            });
            if (indexOfReq != -1) {
              listReq.splice(indexOfReq, 1)
            }
          });
          listReq.forEach(element => {
            list.push(element)
          });
          var count = 0
          list.forEach(element => {
            count += (element.shangpinshuliang * element.shangpinshoujia)
          });
          that.setData({
            shangpinSelect: list,
            zongjine: Number(count),
            modalHidden: true
          })
        },
      })
    } else {
      var list = this.data.shangpinSelect
      var item = this.data.shangpinList[id]
      item.shangpinshuliang = 1
      var check = false
      list.forEach(element => {
        if (element.shangpinbianhao == item.shangpinbianhao) {
          element.shangpinshuliang +=1
          check = true
        }
      });
      if (!check) {
        list.push(item)
      }
      var count = this.data.zongjine+(item.shangpinshuliang*item.shangpinshoujia)
      this.setData({
        shangpinSelect:list,
        zongjine: Number(count),
        modalHidden:true
      })
    }
  },
  toPageFour: function () {
    // app.globalData.order = {
    //   wujinpeitao :this.data.peitaoSelectValue,
    //   shangpin: this.data.shangpinSelect,
    //   zongjine: this.data.zongjine
    // }
    app.globalData.order.wujinpeitao = this.data.peitaoSelectValue
    app.globalData.order.shangpin = this.data.shangpinSelect
    app.globalData.order.zongjine = this.data.zongjine
    app.globalData.order.wujinpeitaoStr = this.data.peitaoSelect
    wx.navigateTo({
      url: '/pages/xinZengDan/order4/order?key=' + this.data.orderCheck,
    })
  },
  onPickerChange(e) {
    // console.log('picker change:', e.detail);
    this.setData({
      peitao2Visible: false,
      peitaoSelectValue: e.detail.value,
      peitaoSelect: e.detail.label,
    });
  },

  onPickerCancel(e) {
    this.setData({
      peitao2Visible: false,
    });
  },
  /**
   * 点击取消
   */
  modalCandel: function (params) {
    // do something
    this.setData({
      modalHidden: true
    })
  },

  /**
    *  点击确认
    */
  modalConfirm: function () {
    // do something
    this.setData({
      modalHidden: true
    })
  },
  onWithoutTitlePicker() {
    this.setData({ peitao2Visible: true });
  },
  goodValueChange: function (params) {
    var list = this.data.shangpinSelect
    var item = list[params.currentTarget.dataset.id]
    item.shangpinshuliang = params.detail.value
    var count = 0
    list.forEach(element => {
      count += (element.shangpinshuliang * element.shangpinshoujia)
    });
    this.setData({
      shangpinSelect: list,
      zongjine: count
    })
  },
  deleteOne: function (params) {
    var id = params.currentTarget.dataset.id
    var list = this.data.shangpinSelect
    list.splice(id, 1)
    var count = 0
    list.forEach(element => {
      count += (element.shangpinshuliang * element.shangpinshoujia)
    });
    this.setData({
      shangpinSelect: list,
      zongjine: count
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})